home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / Amiga / h / getopt.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  4.7 KB  |  180 lines

  1. /*
  2.  * getopt.h : routine to handle options from the argument vector.
  3.  * Source : writen by Thomas Hadig based on getopt.h v1.1 by aklevin
  4.  *
  5.  * Version 1.0, January 1993
  6.  */
  7. /*{{{}}}*/
  8. /*{{{  description*/
  9. /*
  10.  * getopt uses four variables :
  11.  * - optarg points to an option's argument (if any).
  12.  * - optind holds the index of the next argument vector element to parse.
  13.  *     Once all options have been parsed, points to the first non-option
  14.  *     argument. [If (optind > argc) then there are no more arguments].
  15.  * - opterr, if set to 0 will suppress getopt's error messages
  16.  *     (default is 1).
  17.  * - optopt, while not usually documented, is used here to return the actual
  18.  *     option character found, even when getopt itself returns '?'.
  19.  *
  20.  * getopt will parse arguments like :
  21.  * -o
  22.  * -oooooo....
  23.  * -o string
  24.  * -ostring
  25.  *
  26.  * getopt will stop, if there is
  27.  * - no '-' before the next argument of the list (will not be skipped)
  28.  * - is a single '-' (will not be skipped)
  29.  * - is a double '--' (will be skipped)
  30.  *
  31.  */
  32. /*}}}  */
  33.  
  34. #ifndef GETOPT_H
  35.  
  36. /*{{{  defines*/
  37. #define GETOPT_H
  38.  
  39. #ifndef public
  40. #define public
  41. #endif
  42. #ifndef private
  43. #define private static
  44. #endif
  45. /*}}}  */
  46. /*{{{  includes*/
  47. #ifndef _STDIO_H
  48. #include <stdio.h>
  49. #endif
  50. #ifndef _STRING_H
  51. #include <string.h>
  52. #endif
  53. /*}}}  */
  54. /*{{{  variables*/
  55. public char *optarg;   /* contains argument, if needed */
  56. public int optind=1;   /* Index of Argument, where getopt will look in */
  57. public int opterr=1;   /* contains error-print-flag */
  58. public int optopt;     /* contains option char */
  59. /*}}}  */
  60.  
  61. /*{{{  getopt*/
  62. public int getopt (int argc, char **argv, char *optstring)
  63.  {
  64.   int any_more, i=0, result=EOF;
  65.   static int opthold;     /* last line, where looked for option */
  66.   static int optsub=1;    /* store position for second option, if there */
  67.  
  68.   optarg = NULL;
  69.  
  70.   /*{{{  Reset optsub if caller has changed optind*/
  71.   if (optind != opthold) optsub = 1;
  72.   /*}}}  */
  73.   /*{{{  Look at argument vector*/
  74.   while ((!i)&&(optind<argc)&&(result==EOF))
  75.    {
  76.     if ((argv[optind][0] == '-') && (argv[optind][1] != '\0'))
  77.      /*{{{  argument (index optind) contains options ( '-'+chars )*/
  78.      {
  79.       if (argv[optind][1] == '-')
  80.        /*{{{  skip argument '--'*/
  81.        {
  82.         optind++;
  83.         i=1;
  84.        }
  85.        /*}}}  */
  86.       else
  87.        /*{{{  handle option*/
  88.        {
  89.         /*{{{  Look for option ...*/
  90.         i=0;
  91.         while ((i<strlen(optstring))&&(result==EOF))
  92.          {
  93.           if ( (optopt = argv[optind][optsub]) == optstring[i])
  94.            /*{{{  option found*/
  95.            {
  96.             /*{{{  is there a char behind the option ?*/
  97.             any_more = strlen(argv[optind])-optsub-1;
  98.             /*}}}  */
  99.             if (optstring[i+1] == ':')
  100.              /*{{{  argument needed*/
  101.              {
  102.               if (optind == argc-1 && !any_more)
  103.                /*{{{  no arguments and no chars left -> missing argument*/
  104.                {
  105.                 if (opterr) fprintf(stderr,
  106.                   "%s: `-%c' option requires an argument.\n",argv[0], optopt);
  107.                 optind++;
  108.                 result='?';
  109.                }
  110.                /*}}}  */
  111.               else
  112.                /*{{{  set optarg correct and change optind*/
  113.                {
  114.                 /*{{{  chars behind option -> No, use next argument*/
  115.                 if (!any_more) optarg = argv[++optind];
  116.                 /*}}}  */
  117.                 /*{{{  ...Yes*/
  118.                 else optarg = &argv[optind][optsub+1];
  119.                 /*}}}  */
  120.                 optind++;
  121.                 optsub=1;
  122.                }
  123.                /*}}}  */
  124.              }
  125.              /*}}}  */
  126.             else
  127.              /*{{{  No*/
  128.              {
  129.               /*{{{  no chars follow -> next option in next argument*/
  130.               if (!any_more)
  131.                {
  132.                 optind++;
  133.                 optsub=1;
  134.                }
  135.               /*}}}  */
  136.               /*{{{  Yes*/
  137.               else optsub++;
  138.               /*}}}  */
  139.              }
  140.              /*}}}  */
  141.             result=optopt;
  142.            }
  143.            /*}}}  */
  144.           i++;
  145.          }
  146.         i=0;
  147.         /*}}}  */
  148.         if (result==EOF)
  149.          /*{{{  not found*/
  150.          {
  151.           /*{{{  print Unknown option*/
  152.           if (opterr)
  153.             fprintf(stderr, "%s: Unrecognized option `-%c'.\n", argv[0], optopt);
  154.           /*}}}  */
  155.           /*{{{  are there more options ? Yes -> set optsub*/
  156.           if (strlen(argv[optind])-optsub-1) optsub++;
  157.           /*}}}  */
  158.           else
  159.            /*{{{  No*/
  160.            {
  161.             optind++;
  162.             optsub=1;
  163.            }
  164.            /*}}}  */
  165.           result='?';
  166.          }
  167.          /*}}}  */
  168.        }
  169.        /*}}}  */
  170.      }
  171.      /*}}}  */
  172.     else i=1;
  173.    }
  174.   /*}}}  */
  175.   opthold = optind;
  176.   return(result);
  177.  }
  178. /*}}}  */
  179. #endif
  180.